//
//  DateAndTimeSelectionViewController.m
//  MemoryTasks
//
//  Created by Philippe Bouamriou on 17/05/10.
//  Copyright 2010 Philippe Bouamriou. All rights reserved.
//

#import "DateAndTimeSelectionViewController.h"
#import "DateAndTimeSelectionViewController-IB.h"

// Constants used for the tableDataSources
#define kDateRow          0
#define kTimeRow          1
#define kNumberOfRows     2
#define kNumberOfSections 1

// Private Methods
@interface DateAndTimeSelectionViewController (Private)
@end


@implementation DateAndTimeSelectionViewController

@synthesize date, identifier, delegate,_datePicker, _timePicker, _tableView;
@synthesize startDate, endDate;

/*
 // The designated initializer.  Override if you create the controller programmatically and want to perform 
 // customization that is not appropriate for viewDidLoad.
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if ((self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])) {
        // Custom initialization
    }
    return self;
}
*/


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
   if (date) {
      _datePicker.date = date;
      _timePicker.date = date;
   }
   
   if (startDate) {
      _datePicker.minimumDate = startDate;
      _timePicker.minimumDate = startDate;
   }
   
   if (endDate) {
      _datePicker.maximumDate = endDate;
      _timePicker.maximumDate = endDate;
   }
   
   rowSelected = 0;
   // Reload Data essentials for iPhone OS3
   [_tableView reloadData];
   [_tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:rowSelected inSection:0]
                           animated:NO 
                     scrollPosition:UITableViewScrollPositionMiddle];

   [super viewDidLoad];
}


/*
// Override to allow orientations other than the default portrait orientation.
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}
*/

- (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

- (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}



#pragma mark -
#pragma mark Actions


- (IBAction) pickerValueChanged:(id)sender {
   UIDatePicker *datePicker = sender;
   date = datePicker.date;
   [_tableView reloadData];
   [_tableView selectRowAtIndexPath:[NSIndexPath indexPathForRow:rowSelected inSection:0]
                           animated:NO 
                     scrollPosition:UITableViewScrollPositionMiddle];
   if (datePicker == _datePicker) {
      _timePicker.date = date;
   }
   else {
      _datePicker.date = date;
   }

}

#pragma mark -
#pragma mark Implementation of protocol UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
   return kNumberOfSections;
}

- (NSInteger)tableView:(UITableView *)theTableView numberOfRowsInSection:(NSInteger)section {
   return kNumberOfRows;
}


- (UITableViewCell *)tableView:(UITableView *)theTableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
   static NSString *cellIdentifier = @"MyCellDateEdition";
   
   UITableViewCell *cell = [theTableView dequeueReusableCellWithIdentifier:cellIdentifier];
   
   if (cell == nil) {
      cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
   }
   
   if (indexPath.row == kDateRow) {
      cell.textLabel.text = [NSDateFormatter localizedStringFromDate:date 
                                                           dateStyle:NSDateFormatterLongStyle 
                                                           timeStyle:NSDateFormatterNoStyle];
      cell.textLabel.textAlignment = NSTextAlignmentCenter;
   }
   else if(indexPath.row == kTimeRow) {
      cell.textLabel.text = [NSDateFormatter localizedStringFromDate:date 
                                                           dateStyle:NSDateFormatterNoStyle 
                                                           timeStyle:NSDateFormatterShortStyle];
      cell.textLabel.textAlignment = NSTextAlignmentCenter;
   }

   
   return cell;
}

#pragma mark -
#pragma mark Implementation of protocol UITableViewDelegate

// Row selection
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
   rowSelected = indexPath.row;
   if (rowSelected == kTimeRow) {
      _timePicker.hidden = NO;
      _datePicker.hidden = YES;
   }
   else if(rowSelected == kDateRow) {
      _timePicker.hidden = YES;
      _datePicker.hidden = NO;
   }
   
   
}

#pragma mark -
#pragma mark Implementation of protocol CustomNavigationControllerDelegate

- (void)navigationControllerBackButtonPressed {
   [delegate dateSelectionFromDateAndTimeController:date withIdentifier:identifier];
}



@end
